#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void SLInit(SL* psl)
{
	psl->a = (SLDatetype*)malloc(sizeof(SLDatetype) * 4);
	if (psl->a == NULL)
	{
		perror("malloc error");
		return;
	}
	psl->capacity = 4;
	psl->size =0;
}
void SLDestroy(SL* psl)
{
	free(psl->a);
	psl->a = NULL;
	psl->size = 0;
	psl->capacity = 0;
}
void SLPrint(SL* psl)
{
	for (int i = 0; i < psl->size; i++)
	{
		printf("%d ", psl->a[i]);
	}
	printf("\n");
}
SLCheckCapacity(SL*psl)
{
	if (psl->size == psl->capacity)
	{
		SLDatetype*tmp = (SLDatetype*)realloc(psl->a, sizeof(SLDatetype) * psl->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc error");
			return;
		}
		psl->a = tmp;
		psl->capacity *= 2;
	}
	
}
void SLPushBack(SL* psl, SLDatetype x)
{
	/*SLCheckCapacity(psl);
	psl->a[psl->size] = x;
	psl->size++;*/
	SLInsert(psl, psl->size, x);
}
void SLPushFront(SL* psl, SLDatetype x)
{
	/*SLCheckCapacity(psl);
	int end = psl->size - 1;
	while (end >= 0)
	{
		psl->a[end + 1] = psl->a[end];
		end--;
	}
	psl->a[0] = x;
	psl->size++;*/
	SLInsert(psl, 0, x);
}
void SLPopFront(SL* psl)
{
	int start = 0;
	while (start<psl->size-1)
	{
		psl->a[start] = psl->a[start + 1];
		start++;
	}
	psl->size--;
}
void SLPopBack(SL* psl)
{
	assert(psl->size > 0);
	psl->size--;
}
void SLInsert(SL* psl, int pos, SLDatetype x)
{
	SLCheckCapacity(psl);
	int end = psl->size - 1;
	while (end >= pos)
	{
		psl->a[end + 1] = psl->a[end];
		--end;
	}
	psl->a[pos] = x;
	psl->size++;
}
void SLErase(SL* psl, int pose)
{
	assert(0 <= pose && pose <= psl->size);
	int start = pose + 1;
	while (start < psl->size)
	{
		psl->a[start - 1] = psl->a[start];
		++start;
	}
	psl->size--;
}
int SLFind(SL* psl, SLDatetype x)
{
	for (int i = 0; i < psl->size; i++)
	{
		if (psl->a[i] == x)
		{
			return 1;
		}
	}
	return -1;
}
void SLModify(SL* psl, int pos, SLDatetype x)
{
	assert(0 <= pos && pos < psl->size);
	psl->a[pos] = x;
}
